<template>
  <div class="login">
    <el-form :model="loginForm" :rules="fieldRules" ref="loginForm" label-position="left" label-width="0px"
             class="login-container">
      <h2 class="title" style="padding-left:22px;">系统登录</h2>
      <el-form-item prop="userName">
        <el-input type="text" v-model="loginForm.userName" auto-complete="off" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-col :span="12">
          <el-form-item prop="patchca">
            <el-input type="test" v-model="loginForm.patchca" auto-complete="off" placeholder="验证码, 单击图片刷新"
                      style="width: 100%;">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="1">&nbsp;</el-col>
        <el-col :span="11">
          <el-form-item>
            <img style="width: 100%;" class="pointer" :src="loginForm.src" @click="refreshPatchca">
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-col :span="12" style="text-align: left;">
        <el-checkbox v-model="rememberMe" checked class="remember">记住密码</el-checkbox>
      </el-col>
      <el-col :span="12">
      </el-col>
      <el-form-item>
        <el-button type="primary" style="width:100%;" @click.native.prevent="login('loginForm')" :loading="loading">登 录
        </el-button>
      </el-form-item>
    </el-form>
    <vue-particles
      color="#fff"
      :particleOpacity="0.7"
      :particlesNumber="100"
      shapeType="circle"
      :particleSize="4"
      linesColor="#fff"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="3"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
      class="particles"
    >
    </vue-particles>
  </div>
</template>

<script>
export default {
    name: 'Login',
    data() {
        var validateName = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入帐号'))
            } else {
                callback()
            }
        }

        var validatePwd = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'))
            } else {
                callback()
            }
        }

        var validateCode = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入验证码'))
            } else {
                callback()
            }
        }

        return {
            loading: false,
            loginForm: {
                userName: '',
                password: '',
                patchca: '',
                src: ''
            },
            uuid: '',
            rememberMe: true,
            fieldRules: {
                userName: [{ validator: validateName, trigger: blur }],
                password: [{ validator: validatePwd, trigger: blur }],
                patchca: [{ validator: validateCode, trigger: blur }]
            }
        }
    },
    methods: {
        refreshPatchca: function() {
            this.uuid = this.global.uuid()
            this.loginForm.src = this.global.baseUrl + '/patchca/' + this.uuid
        },
        login: function(formName) {
            this.loading = true
            const userInfo = {
                userName: this.loginForm.userName,
                password: this.loginForm.password,
                uuid: this.uuid,
                patchca: this.loginForm.patchca,
                rememberMe: this.rememberMe
            }
            this.$refs[formName].validate((valid) => {
                if (!valid) {
                    this.loading = false
                    return false
                }
                this.$api.login.login(userInfo).then((res) => {
                    if (!res.success) {
                        this.$message({
                            message: res.message,
                            type: 'error'
                        })
                        this.loading = false
                        return
                    }
                    this.loading = false
                    // 保存用户信息
                    sessionStorage.setItem('user', JSON.stringify(res.data))
                    // 登录成功，跳转到主页
                    this.$router.push(this.global.homeUrl)
                }).catch((res) => {
                    this.$message({
                        message: res.message,
                        type: 'error'
                    })
                    this.loading = false
                })
            })
        }
    },
    mounted() {
        this.refreshPatchca()
    }
}
</script>

<style>
  *, :after, :before {
    box-sizing: border-box;
  }
  .particles{
    background: radial-gradient(circle, #5690D2, #c6e2ff);
    height: calc(100vh - 4px);
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }
  .login{
    position: relative;
    -ms-flex-align: center;
    -webkit-box-align: center;
    align-items: center;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    height: 100vh;
    width: 100vw;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
  }
  .login-container {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 200px auto;
    width: 400px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    z-index: 999;
  }
  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #505458;
  }

  .remember {
    margin: 0px 0px 35px 0px;
  }

  .pointer {
    width: 130px;
    height: 40px;
    cursor: pointer;
  }
</style>
